﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using VsxFactory.Modeling.StrategyManager.Strategies;
using EnvDTE;
using Microsoft.VisualStudio.Shell.Interop;
using System.IO;

namespace VsxFactory.Modeling.StrategyManager
{
    class StrategyNameEditor : ExtenderTypeEditor 
    {
        private IWindowsFormsEditorService editorService;

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            ProjectItem pi = GetProjectItem(context);
            if (pi != null)
            {
                var strategies = (object[])pi.Properties.Item("DependencyFileStrategyModelExtender.Strategies").Value;

                editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
                if (editorService == null)
                    return value;

                string strategyName = (string)value;
                string extension = Path.GetExtension(pi.get_FileNames(0));
                var control = new ListBox();
                control.Items.Add(String.Empty);
                foreach (var item in strategies.Cast<Lazy<Modeling.Strategies.IGenerationStrategy, Modeling.Strategies.IStrategyMetadata>>().Where(s => s.Metadata.Extension == null || s.Metadata.Extension.Equals(extension, StringComparison.InvariantCultureIgnoreCase)))
                {
                    control.Items.Add(item.Metadata.Name);
                }
                control.Width += 30;
                control.KeyDown += KeyDown;
                control.Leave += ValueChanged;
                control.DoubleClick += ValueChanged;
                control.Click += ValueChanged;

                editorService.DropDownControl(control);
                if (control.SelectedItem != null)
                    value = control.SelectedItem.ToString();
            }
            return value;
        }


        /// <summary>
        /// Keys down.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.KeyEventArgs"/> instance containing the event data.</param>
        private void KeyDown(object sender, KeyEventArgs e)
        {
            if (((e != null) && e.Control) && ((e.KeyCode == Keys.Return) && (editorService != null)))
            {
                editorService.CloseDropDown();
            }
        }

        /// <summary>
        /// Values the changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ValueChanged(object sender, EventArgs e)
        {
            if (editorService != null)
            {
                editorService.CloseDropDown();
            }
        } 
    }
}
